package com.mttang.util;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mttang.info.core.InfoFileConfigure;

public class InfoFileConfigureUtil {

    private static final Log logger = LogFactory.getLog(InfoFileConfigureUtil.class);

    /**
     * map转为InfoFileConfigure对象
     * 
     * @param map
     * @return
     */
    public static InfoFileConfigure mapToInfo(Map<Object, Object> map) {
        InfoFileConfigure configure = new InfoFileConfigure();

        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            String key = String.valueOf(entry.getKey());
            String first = key.charAt(0) + "";
            key = first.toUpperCase() + key.substring(1);
            String methodName = "set" + key;
            Object v = entry.getValue();
            try {
                Method meth = InfoFileConfigure.class.getMethod(methodName, String.class);// 调用set函数封装
                meth.invoke(configure, v != null ? v : "");
            } catch (Exception e) {
                if (logger.isDebugEnabled()) {
                    logger.debug(e.getMessage() + "-> 方法名：" + methodName + "值：" + v);
                }
            }
        }
        return configure;
    }

    /**
     * 复制基本信息至InfoFileConfigure
     * 
     * @param copy 目标对象
     * @param src 复制源
     * @return
     */
    public static InfoFileConfigure copyBaseToInfo(InfoFileConfigure copy, InfoFileConfigure src) {
        // 复制注入的基本值
        InfoFileConfigure temp = src;
        copy.setImagePath(temp.getImagePath());
        copy.setImageType(temp.getImageType());
        copy.setFlashPath(temp.getFlashPath());
        copy.setFlashType(temp.getFlashType());
        copy.setMeadiaPath(temp.getMeadiaPath());
        copy.setMeadiaType(temp.getMeadiaType());
        copy.setJsPath(temp.getJsPath());
        copy.setJsType(temp.getJsType());
        copy.setCssPath(temp.getCssPath());
        copy.setCssType(temp.getCssType());
        copy.setCnfPath(temp.getCnfPath());
        copy.setCnfType(temp.getCnfType());
        copy.setCommonPath(temp.getCommonPath());
        copy.setFileTemplatePath(temp.getFileTemplatePath());
        copy.setFileTemplateWeb(temp.getFileTemplateWeb());
        copy.setHtmlTemplatePath(temp.getHtmlTemplatePath());
        copy.setHtmlTemplateWeb(temp.getHtmlTemplateWeb());
        return copy;
    }
}
